var models = require('./db');//数据库链接信息
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var $sql = require('./sqlfun');//sql语句

// 连接数据库
var conn = mysql.createConnection(models.mysql);
conn.connect();
// 获取站点信息
router.get('/site', (req, res) => {
  var sql = $sql.site.site;
  conn.query(sql, function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      var data = {
        status: 200,
        data: result
      }
      res.end(JSON.stringify(data));
    } else {
      var data = {
        status: 200,
        msg: '暂无数据'
      }
      res.end(JSON.stringify(data))
    }
  })
});
// 获取社交信息
router.get('/social', (req, res) => {
  var sql = $sql.social.social;
  conn.query(sql, function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      var data = {
        status: 200,
        data: result
      }
      res.end(JSON.stringify(data));
    } else {
      var data = {
        status: 200,
        msg: '暂无数据'
      }
      res.end(JSON.stringify(data))
    }
  })
});
// 获取焦点文章
router.get('/focus/list', (req, res) => {
  var sql = $sql.article.focusList;
  conn.query(sql, function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      var data = {
        status: 200,
        data: result
      }
      res.end(JSON.stringify(data));
    } else {
      var data = {
        status: 200,
        msg: '暂无数据'
      }
      res.end(JSON.stringify(data))
    }
  })
});
// 获取推荐文章
router.get('/post/list', (req, res) => {
  var sql = $sql.article.postList;
  var params = req.query
  var m = (params.page - 1) * params.size, n = Number(params.size)
  conn.query(sql, [m, n], function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      var data = {
        status: 200,
        data: result,
        total: result.length,
        hasNextPage: params.size * params.page < result.length
      }
      res.end(JSON.stringify(data));
    } else {
      var data = {
        status: 200,
        msg: '暂无数据',
        total: 0,
        hasNextPage: false
      }
      res.end(JSON.stringify(data))
    }
  })
});
// 获取文章列表
router.get('/post/list/all', (req, res) => {
  var sql = $sql.article.all;
  var params = req.query
  var m = (params.page - 1) * params.size, n = Number(params.size)
  var data = {
    status: 200,
    data: [],
  }
  conn.query(sql, [m, n], function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      data = {
        status: 200,
        data: result,
      }
      conn.query($sql.article.total, function (err, resulTotol) {
        if (err) {
          console.log(err);
        }
        if (resulTotol) {
          data.total = resulTotol[0].total
          data.hasNextPage = params.size * params.page < resulTotol[0].total
          res.end(JSON.stringify(data))
        }
      })
    } else {
      data = {
        status: 200,
        msg: '暂无数据',
        total: 0,
        hasNextPage: false
      }
      res.end(JSON.stringify(data))
    }
  })
});
// 获取友链数据
router.get('/friend', (req, res) => {
  var sql = $sql.friend.friend;
  conn.query(sql, function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      var data = {
        status: 200,
        data: result,
      }
      res.end(JSON.stringify(data));
    } else {
      var data = {
        status: 200,
        msg: '暂无数据',
      }
      res.end(JSON.stringify(data))
    }
  })
});
// 后台登录用户接口
router.post('/admin/login', (req, res) => {
  res.writeHead(200, { 'content-type': 'text/plain;charset=utf-8' });
  var sql = $sql.user.login;
  var params = req.query;
  conn.query(sql, [params.username], function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      console.log(result)
      if (result.length > 0) {
        for (var i = 0; i < result.length; i++) {
          if (result[i].pwd == params.password) {
            var data = {
              status: 200,
              msg: '登录成功',
            }
            res.end(JSON.stringify(data));
          } else {
            var data = {
              status: 502,
              msg: '用户名或密码错误！',
            }
            res.end(JSON.stringify(data));
          }
        }
      } else {
        dataError2 = {
          status: '401',
          msg: '系统中无此账号！,请先注册'
        }
        res.end(JSON.stringify(dataError2));
      }
    }
  })
});
// 后台文章新增接口
router.post('/admin/save', (req, res) => {
  res.writeHead(200, { 'content-type': 'text/plain;charset=utf-8' });
  var sql = $sql.admin.save;
  var params = req.body;
  conn.query(sql, [params.title, params.file, params.isTop, params.file, params.isHot, params.pubtime, params.summary, params.content, params.viewsCount, params.commentsCount, params.focus, params.recommend, params.html], function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      if (result.affectedRows == 1) {
        res.end(JSON.stringify({ status: '200', msg: '新增成功' }));
      } else {
        res.end(JSON.stringify({ status: '500', msg: '新增失败' }));
      }
    } else {
      res.end(JSON.stringify({ status: '300', msg: '已存在文章' }));
    }
  })
});
// 文章详情接口
router.get('/detail', (req, res) => {
  res.writeHead(200, { 'content-type': 'text/plain;charset=utf-8' });
  var sql = $sql.article.detail;
  var params = req.query;
  console.log(params)
  conn.query(sql, params.id, function (err, result) {
    if (err) {
      console.log(err);
    }
    if (result) {
      res.end(JSON.stringify({ status: 200, data: result }));
    } else {
      res.end(JSON.stringify({ status: '500', msg: '失败' }));
    }
  })
});
module.exports = router;